#ifndef YANZHENGMA_H
#define YANZHENGMA_H

#include <QObject>
#include <QTcpSocket>
#include <QDebug>
#include <QTextStream>
#include <QCoreApplication>
#include<QRandomGenerator>

class EmailService : public QObject {
    Q_OBJECT
public:
    explicit EmailService(QObject *parent = nullptr) : QObject(parent) {}

    static QString generateCaptcha() {
        // 生成随机验证码的逻辑
        return QString::number(QRandomGenerator::global()->bounded(100000, 999999));
    }


    static bool verifyCode(const QString& inputCode, const QString& correctCode) {
           // 比较用户输入的验证码和正确的验证码是否一致
           return inputCode == correctCode;
       }

    static bool sendCaptchaEmail(const QString& recipient, const QString& captcha) {
        QTcpSocket socket;
        socket.connectToHost("smtp.qq.com", 25); // 使用普通的 SMTP 端口 25

        if (!socket.waitForConnected()) {
            qDebug() << "Failed to connect to SMTP server";
            return false;
        }

        // 读取服务器返回的欢迎消息
        if (!socket.waitForReadyRead()) {
            qDebug() << "Failed to read welcome message from SMTP server";
            return false;
        }
        qDebug() << "Server response:" << socket.readAll();

        // 发送 EHLO 命令
        socket.write("EHLO localhost\r\n");
        socket.waitForBytesWritten();
        if (!socket.waitForReadyRead()) {
            qDebug() << "Failed after EHLO command";
            return false;
        }
        qDebug() << "Sent EHLO command";
        qDebug() << "Server response after EHLO:" << socket.readAll();

        // 发送 AUTH LOGIN 命令
        socket.write("AUTH LOGIN\r\n");
        socket.waitForBytesWritten();
        if (!socket.waitForReadyRead()) {
            qDebug() << "Failed after AUTH LOGIN command";
            return false;
        }
        qDebug() << "Sent AUTH LOGIN command";
        qDebug() << "Server response after AUTH LOGIN:" << socket.readAll();

        // 发送 Base64 编码的用户名
        QByteArray username = QByteArray("2645055534@qq.com").toBase64();
        socket.write(username + "\r\n");
        socket.waitForBytesWritten();
        if (!socket.waitForReadyRead()) {
            qDebug() << "Failed after sending username";
            return false;
        }
        qDebug() << "发送 username";
        qDebug() << "输入username后的服务器响应:" << socket.readAll();

        // 发送 Base64 编码的授权码
        QByteArray password = QByteArray("zklhrlgumsvhdjag").toBase64();
        socket.write(password + "\r\n");
        socket.waitForBytesWritten();
        if (!socket.waitForReadyRead()) {
            qDebug() << "发送密码失败";
            return false;
        }
       // qDebug() << "发送密码";
        //qDebug() << "输入密码后的服务器响应:" << socket.readAll();

        // 发送 MAIL FROM 命令
        socket.write("MAIL FROM:<2645055534@qq.com>\r\n");
        socket.waitForBytesWritten();
        if (!socket.waitForReadyRead()) {
            qDebug() << "Failed after MAIL FROM command";
            return false;
        }
       // qDebug() << "Sent MAIL FROM command";
       // qDebug() << "Server response after MAIL FROM:" << socket.readAll();

        // 发送 RCPT TO 命令
        socket.write(QString("RCPT TO:<%1>\r\n").arg(recipient).toUtf8());
        socket.waitForBytesWritten();
        if (!socket.waitForReadyRead()) {
            qDebug() << "Failed after RCPT TO command";
            qDebug() << "Server response after RCPT TO:" << socket.readAll();
            return false;
        }
       // qDebug() << "Sent RCPT TO command";
        //qDebug() << "Server response after RCPT TO:" << socket.readAll();

        // 发送 DATA 命令
        socket.write("DATA\r\n");
        socket.waitForBytesWritten();
        if (!socket.waitForReadyRead()) {
            qDebug() << "Failed after DATA command";
            return false;
        }
        //qDebug() << "Sent DATA command";
       // qDebug() << "Server response after DATA:" << socket.readAll();

        // 发送邮件头部和正文
        socket.write("From: <2645055534@qq.com>\r\n");
        socket.write(QString("To: <%1>\r\n").arg(recipient).toUtf8());
        socket.write("Subject: Verification Code\r\n");
        socket.write("Content-Type: text/plain; charset=utf-8\r\n");
        socket.write("\r\n");  // 头部结束，正文开始
        socket.write(QString("出入库管理系统，你的验证码是: %1\r\n").arg(captcha).toUtf8());
        socket.write(".\r\n");  // 邮件正文结束标志
        socket.waitForBytesWritten();
        if (!socket.waitForReadyRead()) {
            qDebug() << "Failed after sending email body";
            return false;
        }
       // qDebug() << "Sent email body";
        //qDebug() << "Server response after email body:" << socket.readAll();

        // 发送 QUIT 命令
        socket.write("QUIT\r\n");
        socket.waitForBytesWritten();
        if (!socket.waitForReadyRead()) {
            qDebug() << "Failed after QUIT command";
            return false;
        }
        qDebug() << "Sent QUIT command";
        qDebug() << "Server response after QUIT:" << socket.readAll();

        socket.close(); // 关闭连接
        return true;
    }
};

#endif // MAIL_CODE_H
